package com.woniuxy.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.user.dao.UserDao;
import com.woniuxy.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;


    @Override
    public ResponseResult<List<User>> findAll() {
        List<User> all = userDao.findAll();
        return new ResponseResult<>(200, "成功", all, ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userDao.findAll();
        PageInfo<User> pageInfo = PageInfo.of(list);
        return new ResponseResult(200, "成功", pageInfo, ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteOne(int id) {
        userDao.deleteOne(id);
        return new ResponseResult(200, "删除成功", null, ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult findByRecord(int id) {
        User one = userDao.findByRecord(id);
        return new ResponseResult(200, "成功", one, ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult findByAddress(int id) {
        User one = userDao.findByAddress(id);
        return new ResponseResult<>(200,"成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult findByUsername(String username) {
        return null;
    }

    @Override
    public ResponseResult findByName(String name) {
        User one=userDao.findByName(name);
        return new ResponseResult(200,"成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteSome(int[] id) {
        userDao.deleteSome(id);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult findByTime(String time) {
        return null;
    }

    @Override
    public ResponseResult findStatus(int id) {
        int i=userDao.findStatus(id);
        return new ResponseResult(200,"成功",i,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult updateStatus(User user) {
        userDao.updateStatus(user);
        return new ResponseResult(200,"修改成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<PageInfo<User>> findByCondition(QueryEntity<User> queryEntity) {
        //开启分页
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        //排序
        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }
        //封装数据
        List<User> all=userDao.findByCondition(queryEntity.getEntity());
        PageInfo<User> pageInfo=PageInfo.of(all);
        return new ResponseResult<>(200,"成功",pageInfo,ResStatus.SUCCESS);
    }


}
